<template>
	<view class="">
		<qiun-title-bar title="博客类别比例"></qiun-title-bar>
		<view class="charts-box">
			<qiun-data-charts type="pie" :chart-data="pie"
				:opts="{legend: {float: 'left', fontSize: 12}}"></qiun-data-charts>
		</view>
		<qiun-title-bar title="最新博客分析"></qiun-title-bar>
		<view class="charts-box">
			<qiun-data-charts type="column" :chart-data="column" :ontouch="true" :canvas2d="true" canvasId="aba54912"
				:opts="{enableScroll:true,xAxis:{scrollShow:true,itemCount:4,disableGrid:true, fontSize: 10}}"></qiun-data-charts>
		</view>
		<qiun-title-bar title="常用标签词云"></qiun-title-bar>
		<view class="charts-box">
			<qiun-data-charts type="word" :chart-data="word"></qiun-data-charts>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pie: {},
				column: {},
				word: {}
			}
		},
		onLoad() {
			this.getCategories()
			this.getPopBlogs()
			this.getTags()
		},
		methods: {
			getTags() {
				this.word = {
					series: [

					]
				}
				let url = this.$params.host + this.$params.action_tags
				this.$req.get(url, res => {
					// console.log(res)
					url = this.$params.host + this.$params.action_tag_count
					res.data.data.forEach(async (t) => {
						let r = await this.$req.request(url + t.id)
						// console.log(r)
						if (r.success && r.data > 0) {
							let tag = {
								name: t.name,
								textSize: parseInt(r.data) * 10
							}
							this.word.series.push(tag)
						}
					})
				})
			},
			getPopBlogs() {
				this.column = {
					categories: [],
					series: [{
						name: "阅读量",
						data: []
					}, {
						name: "评论数",
						data: []
					}]
				}
				let url = this.$params.host + this.$params.action_top_blogs
				this.$req.get(url, res => {
					// console.log(res)
					res.data.data.forEach(b => {
						if (b.title.length > 10) {
							b.title = b.title.substr(0, 8) + "..."

						}
						this.column.categories.push(b.title)
						this.column.series[0].data.push(b.readCount)
						this.column.series[1].data.push(b.comments.length)
					})
				}, )
			},
			getCategories() {
				this.pie = {
					series: [{
						data: []
					}]
				}
				let url = this.$params.host + this.$params.action_categories
				this.$req.get(url, res => {
					this.categories = []
					url = this.$params.host + this.$params.action_category_count
					res.data.data.forEach(async (c) => {
						let r = await this.$req.request(url + c.id)
						//console.log(r);
						if (r.success) {
							let category = {
								name: c.name,
								value: r.data
							}
							this.pie.series[0].data.push(category)
						}
					})
				})
			}
		}
	}
</script>

<style>

</style>